<template>
<div>
<el-row>

<el-form :inline="true" >
	<el-form-item >
		<daterange v-bind:se.sync="s_e" type="daterange"></daterange>
    </el-form-item>

	<el-form-item>
	    <el-button type="primary" @click="onSubmit" :loading="btnloading">查询</el-button>
	</el-form-item>

	<el-form-item>
	    <el-button type="success" @click="toExcel">导出Excel</el-button>
	</el-form-item>

</el-form>
</el-row>

<el-row>
<el-table
    :data="objs"
    border
    show-summary
    v-loading="loading"
    style="width: 100%">
    <el-table-column
      prop="name"
      label="产品名称"
      >
    </el-table-column>

    <el-table-column
      prop="attr_name"
      label="型号名称"
      width="300"
      >
    </el-table-column>

    <el-table-column
      prop="count"
      label="订单数量"
      width="120"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="num"
      label="出票数量"
      width="120"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="used"
      label="使用数量"
      width="120"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="back"
      label="退票数量"
      width="120"
      sortable
      >
    </el-table-column>
    
</el-table>
</el-row>

</div>
</template>

<script>

import { mapGetters } from 'vuex'
import { date2str, dateDiff } from '../../../config/util'
import daterange from '../../../components/daterange'

export default {
	
	name : 'book',
	
	data () {
		return {
		  	s_e : [date2str(new Date()), date2str(new Date())],
		  	loading : false,
		  	filename : '',

		  	btnloading : false,
		}
	},

	props : {

	},

	components : {
		daterange,
	},

	computed : {
		...mapGetters([
			'book',
			'userInfo',
		]),

		objs () {
			let statistics = {};
			let arr = [];
			const length = this.book.length;
			for (let i = 0; i < length; i++){
				let tmp = this.book[i];
				const key = tmp.product_code + '-' + tmp.attr_id;
				if(statistics[key] == undefined){
					statistics[key] = {
                        'name' : tmp.title,
                        'attr_name' : tmp.attr_name,
                        'product_code' : tmp.product_code,
                        'total_price' : tmp.total_price,    //实际收入
                        'count' : 1,
                        'num' : tmp.num,
                        'used' : tmp.used,
                        'back' : tmp.back,
                    };
				}else{
					statistics[key].count += 1;
                    statistics[key].num += parseInt(tmp.num);
                    statistics[key].used += parseInt(tmp.used);
                    statistics[key].back += parseInt(tmp.back);
				}
			}

            for (let key1 in statistics) {
				arr.push(statistics[key1]);
			}

            return arr;
		},
		
	},

	methods : {
		getData() {

			const day = dateDiff(this.s_e[0], this.s_e[1]);
			
			if(day > 30){
                this.$message.warning('时间跨度不能超过30天');
                return;
			}

			this.loading = true;
			this.btnloading = true;

			this.filename = '预定报表' + '[' + this.s_e[0] + '~' + this.s_e[1] + ']';

			let para = {
				fun : 'book',
				start_time : this.s_e[0] + ' 00:00:00',
				end_time : this.s_e[1] + ' 23:59:59',
				cb : res => {
					this.loading = false;
					this.btnloading = false;
				},
				err : res => {
					this.loading = false;
					this.btnloading = false;
					this.$notify.error({
			          title: '错误',
			          message: res,
			        });
				}
			};
			this.$store.dispatch('query', para);
		},

		onSubmit () {
			this.getData();
		},

		toExcel (){
			
			const labels = ['code', 'title', 'attr_name', 'create_time', 'pay_time',
			'name', 'mobile', 'cardno', 'group', 'uid_nickname', 'remark', 'seller_remark', 
			'guide_price', 'pay_fee', 'num', 'used', 'back',
			'pay_trade_no', 
			'nickname', 'profit', 'nickname1', 'profit1', 'voucher_price'];
	        let arr = [];
	        for(let i = 0; i< this.book.length; i++){
	            let tmp = this.book[i];
	            let haha = [];
	            for(let j = 0; j < labels.length; j++){
	                haha.push(tmp[labels[j]]);
	            }
	            arr.push(haha);
	        }
	        arr.splice(0, 0, ['订单编号', '订单名称', '型号名称', '下单时间' , '支付时间', 
	        	'姓名', '电活', '身份证', '团期', '下单人微信昵称', '买家备注', '卖家备注', 
	        	'单价', '支付金额', '购买数量', '核销数量', '退票数量', 
	        	'支付流水号', 
	            '一级昵称', '一级分佣', '二级昵称', '二级分佣', '优惠券金额'
	            ]);

	        ExportUtil.toExcel(arr, this.filename);

		}
	},

	watch : {
	    
	},

	mounted () {
		this.getData();
	},

}
</script>

<style scoped>

</style>
